package com.junsi.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.Client;
import com.junsi.bean.CompactGenre;
import com.junsi.bean.Contract;
import com.junsi.bean.Department;
import com.junsi.bean.Sale;
import com.junsi.bean.Staff;
import com.junsi.service.ClientService;
import com.junsi.service.CompactGenreService;
import com.junsi.service.ContractService;
import com.junsi.service.DepartmentSerivce;
import com.junsi.service.SaleService;
import com.junsi.service.StaffSerivce;
import com.junsi.util.InteractiveUtil;
import com.junsi.util.PageUtil;

@Controller
@RequestMapping("/contract")
public class ContractController {
	
	@Autowired
	private ContractService contractService;
	
	@Autowired
	private CompactGenreService compactGenreService;
	
	@Autowired
	private ClientService clientService;
	
	@Autowired
	private DepartmentSerivce departmentSerivce;
	
	@Autowired
	private StaffSerivce staffSerivce;
	
	@Autowired
	private SaleService saleService;
	
	
	
	
	
	/**
	 * 新增合同
	 * @param contract
	 * @return
	 */
	@RequestMapping("saveContract")
	public void saveContract(Contract contract,HttpServletResponse response){
		
		if(contract != null){
			if(contract.getClientName() != null && contract.getContractTitle() != null &&
					 contract.getContractAmount() != null && contract.getContractStartDate() != null
					 && contract.getContractCloseDate() != null && contract.getStaffName() != null && contract.getDepartmentName() != null ){
				boolean reuslt = contractService.save(contract);
				if(reuslt){
					InteractiveUtil.WriteStringForHtml("新增成功", response);
				}else{
					InteractiveUtil.WriteStringForHtml("新增失败", response);
				}
			}else{
				InteractiveUtil.WriteStringForHtml("新增失败", response);
			}
		}else{
			InteractiveUtil.WriteStringForHtml("新增失败", response);
		}
		
	}
	
	/**
	 * 修改合同信息
	 * @param contract
	 * @return
	 */
	@RequestMapping("/updateContract")
	public void updateContract(Contract contract,HttpServletResponse response){
		boolean reuslt = contractService.update(contract);
		if(reuslt){
			InteractiveUtil.WriteStringForHtml("修改成功", response);
		}else{
			InteractiveUtil.WriteStringForHtml("修改失败", response);
		}
	}
	
	/**
	 * 有权限查看的所有合同
	 * @param pageUtil
	 * @param request
	 * @return
	 */
	@RequestMapping("/findByAll")
	public ModelAndView findByAll(PageUtil pageUtil,HttpServletRequest request){
		
		ModelAndView modelAndView = new ModelAndView();
		
		String staff_id = (String)request.getSession().getAttribute("staffs");
		Integer number = contractService.findCount(staff_id);
		if(number != null && number != 0){
			pageUtil.setRowCount(number);
			String staffName = staffSerivce.findByName(staff_id);
			List<Contract> contracts = contractService.findByAll(staffName,pageUtil);
			modelAndView.addObject("contracts", contracts);
			modelAndView.addObject("page", pageUtil);
		}
		modelAndView.setViewName("/");//--------------------
		return modelAndView;
	}
	
	/**
	 * 查看详情
	 * @param id
	 * @return
	 */
	@RequestMapping("findById")
	public void findById(Integer id,HttpServletResponse response){
		Contract contract = contractService.findByID(id);
		InteractiveUtil.WriteBeanForHtml(contract, response);;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/deletes", produces = "application/json; charset=utf-8")
	public void deletes(String ids,HttpServletResponse response){
		
		boolean reuslt = contractService.deletes(ids);
		if(reuslt){
			InteractiveUtil.WriteStringForHtml("删除成功", response);
		}
		
		InteractiveUtil.WriteStringForHtml("删除成功", response);
	}
	
	/**
	 * 客户下拉框
	 * @param request
	 * @param response
	 */
	@RequestMapping("/findClients")
	public void findClients(HttpServletRequest request,HttpServletResponse response){
		Staff staff = (Staff) request.getSession().getAttribute("staff");
		List<Client> clients = clientService.findByStaffId(staff.getStaff_id());
		InteractiveUtil.WriteJsonForHtml(clients, response);
		
	}
	
	/**
	 * 合同下拉框
	 * @param response
	 */
	@RequestMapping("findDepartments")
	public void findDepartments(HttpServletResponse response){
		List<Department> departments = departmentSerivce.findByAll();
		InteractiveUtil.WriteJsonForHtml(departments, response);
	}
	
	/**
	 * 合同类型下拉框
	 * @param response
	 */
	@RequestMapping("/findCompactGenres")
	public void findCompactGenres(HttpServletResponse response){
		List<CompactGenre> compactGenres = compactGenreService.listAll();
		InteractiveUtil.WriteJsonForHtml(compactGenres, response);
	}
	
	/**
	 * 进入合同界面
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/findByAlls")
	public ModelAndView findByAll(HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView();
		String staff_id = (String)request.getSession().getAttribute("staffs");
		String[] staff_ids = staff_id.split(",");
		String staff_name = "";
		for(int i = 0,l = staff_ids.length;i < l;i++){
			Integer id = Integer.valueOf(staff_ids[i]);
			Staff staff = staffSerivce.findById(id);
			if(staff!= null){
				staff_name += staff.getStaff_name()+",";
			}
		}
		staff_name = staff_name.substring(0,staff_name.length()-1);
		String[] staff_names = staff_name.split(",");
		List<Contract> contracts = contractService.findByAll(staff_names);
		modelAndView.addObject("contracts", contracts);
		modelAndView.setViewName("forward:/html/pact1.jsp");
		
		return modelAndView;
	}
	
	@RequestMapping(value="/findStaffSession")
	public void findStaffSession(HttpServletRequest request,HttpServletResponse response){
		Staff staff = (Staff)request.getSession().getAttribute("staff");
		InteractiveUtil.WriteBeanForHtml(staff, response);
	}
	
	@RequestMapping("/findxiaoshoujihui")
	public void findxiaoshoujihui(HttpServletResponse response,String id,HttpServletRequest request){
		String staff_id = (String)request.getSession().getAttribute("staffs");
		String[] staff_ids = staff_id.split(",");
		
		List<Sale> slaes = saleService.findByKeHu(staff_ids,Integer.valueOf(id));
		InteractiveUtil.WriteJsonForHtml(slaes, response);
	}
	
	@RequestMapping(value="/selectById")
	public ModelAndView selectById(Integer id,HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView();
		Contract contract = contractService.findByID(id);
		if(contract != null && contract.getSaleId() != null){
			Sale sale = saleService.findByID(contract.getSaleId());
			modelAndView.addObject("sale", sale);
		}
		Staff staff = (Staff) request.getSession().getAttribute("staff");
		List<Client> clients = clientService.findByStaffId(staff.getStaff_id());
		List<Department> departments = departmentSerivce.findByAll();
		List<CompactGenre> compactGenres = compactGenreService.listAll();
		String staff_id = (String)request.getSession().getAttribute("staffs");
		String[] staff_ids = staff_id.split(",");
		List<Sale> slaes = saleService.findByKeHu(staff_ids,id);
		modelAndView.addObject("clients", clients);
		modelAndView.addObject("departments", departments);
		modelAndView.addObject("compactGenres", compactGenres);
		modelAndView.addObject("slaes", slaes);
		modelAndView.addObject("contract", contract);
		modelAndView.setViewName("forward:/html/pact2.jsp");
		return modelAndView;
	}
}
